package com.client.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.client.bean.BaseResponseBean;
import com.client.utility.HttpResponseUtility;
import com.client.utility.JsonUtility;
import com.fasterxml.jackson.databind.JsonNode;
import com.ning.http.client.Response;
import com.serverclient.cglib.service.ServerCglibService;
import com.serverclient.jdk.service.ServerJdkService;

/**
 * 客户端的业务层
 */
@Service("clientService")
public class ClientService {
	@Autowired
	private ServerCglibService serverCglibService;
	@Autowired
	private ServerJdkService serverJdkService;
	
	public String getJdkUserInfo(String phoneNumber) {
		Map<String,String> param = new HashMap<String, String>();
		param.put("phoneNumber", phoneNumber);
		Map<String,String> header = new HashMap<String, String>();
		header.put("userAgent", "WAP");
		
		Response response = serverJdkService.getUserInfo(param,header);
		
		BaseResponseBean<JsonNode> responseBean = HttpResponseUtility.getResponse(response, JsonNode.class);
		if(responseBean.getCode() == BaseResponseBean.SUCCESS) {
			return JsonUtility.entityToStr(responseBean.getResult());
		}else {
			return "接口调用失败";
		}
	}
	
	public String getCglibUserInfo(String phoneNumber) {
		Map<String,String> param = new HashMap<String, String>();
		param.put("phoneNumber", phoneNumber);
		Map<String,String> header = new HashMap<String, String>();
		header.put("userAgent", "WAP");
		
		Response response = serverCglibService.request("getUserInfo", param, header);
		
		BaseResponseBean<JsonNode> responseBean = HttpResponseUtility.getResponse(response, JsonNode.class);
		if(responseBean.getCode() == BaseResponseBean.SUCCESS) {
			return JsonUtility.entityToStr(responseBean.getResult());
		}else {
			return "接口调用失败";
		}
	}
	
	public String addJdkUserInfo(String phoneNumber) {
		List<String> userList = new ArrayList<String>();
		userList.add(phoneNumber);
		Map<String,String> header = new HashMap<String, String>();
		header.put("userAgent", "WAP");
		
		Response response = serverJdkService.addUserInfo(userList,header);
		BaseResponseBean<JsonNode> responseBean = HttpResponseUtility.getResponse(response, JsonNode.class);
		if(responseBean.getCode() == BaseResponseBean.SUCCESS) {
			return JsonUtility.entityToStr(responseBean.getResult());
		}else {
			return "接口调用失败";
		}
	}
	
	public String addCglibUserInfo(String phoneNumber) {
		List<String> userList = new ArrayList<String>();
		userList.add(phoneNumber);
		Map<String,String> header = new HashMap<String, String>();
		header.put("userAgent", "WAP");
		
		Response response = serverCglibService.request("addUserInfo", userList, header);
		BaseResponseBean<JsonNode> responseBean = HttpResponseUtility.getResponse(response, JsonNode.class);
		if(responseBean.getCode() == BaseResponseBean.SUCCESS) {
			return JsonUtility.entityToStr(responseBean.getResult());
		}else {
			return "接口调用失败";
		}
	}
}
